package com.yuju.roomStatus.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.sun.org.apache.regexp.internal.RE;
import com.yuju.enums.ReserveRoomStatusEnum;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.exception.BusinessException;
import com.yuju.order.enums.OrderAccountReturnCodeEnum;
import com.yuju.order.enums.ReserveStatusEnum;
import com.yuju.order.enums.RoomStatusReturnCodeEnum;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.ReserveRoomModel;
import com.yuju.order.service.IReserverService;
import com.yuju.roomStatus.dao.IRoomTypeStatusDao;
import com.yuju.roomStatus.enums.RoomStatusPrintEnum;
import com.yuju.roomStatus.model.CheckinRoomModel;
import com.yuju.roomStatus.model.RoomStatusPrintModel;
import com.yuju.roomStatus.model.RoomTypeStatusModel;
import com.yuju.roomStatus.service.IRoomTypeStatusService;
import com.yuju.rpt.model.BusinessDailyModel;
import com.yuju.util.DateUtil;
import com.yuju.util.ExcelExp;
import com.yuju.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.enums.ForwardRoomStatusEnum;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.service.IRoomService;
import com.yuju.roomStatus.dao.IRoomStatusDao;
import com.yuju.roomStatus.model.RoomStatusModel;
import com.yuju.roomStatus.service.IRoomStatusService;

/**
 * 
 * 类名：RoomStatusServiceImpl  <br />
 *
 * 功能：房间状态
 *
 * @author : jiangp <br />
 * @Date : 2017年12月13日 下午6:14:27  <br />
 * @version : 1.1 <br />
 */
@Service
public class RoomStatusServiceImpl implements IRoomStatusService {

	@Autowired
	private IRoomStatusDao roomStatusDao;
	
	@Autowired
	private IRoomService roomService;

	@Autowired
	private IRoomTypeStatusService roomTypeStatusService;

	@Autowired
	private IReserverService reserverService;

	/**
	 * 
	 * 功能：从房态中筛选可预订房态<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private ReserveRoomStatusEnum getReserveRoomStatus(RoomStatusModel roomStatusModel) {
		//ReserveRoomStatusEnum status = ReserveRoomStatusEnum.NO_RESERVE;
		if (roomStatusModel.equals(null)) {
			return ReserveRoomStatusEnum.NO_RESERVE;
		}

		//预离房
		if (roomStatusModel.getIsDueOut().equals(StatusEnum.TRUE.getValue())) {
			return ReserveRoomStatusEnum.YULI_FANG;
		}

		//在住，维修，锁房为不可预订
		if (roomStatusModel.getIsLive().equals(StatusEnum.TRUE.getValue())
				|| roomStatusModel.getIsRecord().equals(StatusEnum.TRUE.getValue())
				|| roomStatusModel.getIsLock().equals(StatusEnum.TRUE.getValue())) {
			return ReserveRoomStatusEnum.NO_RESERVE;
		}

		//预订房
		if (roomStatusModel.getIsReserve().equals(StatusEnum.TRUE.getValue())) {
			return ReserveRoomStatusEnum.NO_RESERVE;
		}

		//脏房，净房
		if (roomStatusModel.getIsDirty().equals(StatusEnum.TRUE.getValue())) {
			return ReserveRoomStatusEnum.ZANG_FANG;
		}
		else {
			return ReserveRoomStatusEnum.JING_FANG;
		}
	}

	/**
	 *
	 * 功能：获得远期房态需要的星期几格式<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private static String getWeekFormat(Date dt) {
		String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

	private static Integer INVALID_DAY_INDEX = -1;
	/**
	 *
	 * 功能：远期房态的日期搜索<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private static int getDayIndex(Date day, int dayCount) {
		if (day == null) {
			return INVALID_DAY_INDEX;
		}
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date fromDay = new Date();
		Date toDay = day;

        int dayIndex = (int)DateUtil.getDiffDay(sdf.format(toDay), sdf.format(fromDay));
		if (dayIndex < 0) {
			return 0;
		}
		else if (dayIndex >= dayCount) {
			return dayCount;
		}

		return dayIndex;
	}

	/**
	 *
	 * 功能：判断是否可入住房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private Boolean isCheckinEnableRoom(RoomStatusModel roomStatusModel, String reserveId) {
		if (roomStatusModel == null) {
			return false;
		}

		//在住，锁房，维修下不能入住
		if (roomStatusModel.getIsLive() == StatusEnum.TRUE.getValue()
				|| roomStatusModel.getIsLock() == StatusEnum.TRUE.getValue()
				|| roomStatusModel.getIsRecord() == StatusEnum.TRUE.getValue()) {
			return false;
		}

		//预订情况下，必须是该预订单
		if (roomStatusModel.getIsReserve() == StatusEnum.TRUE.getValue()) {
			if (reserveId != null && !reserveId.equals(roomStatusModel.getReserveId())) {
				return false;
			}
		}

		return true;
	}

	/**
	 *
	 * 功能：房态筛选，条件之间是且的关系<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private boolean isStatus(RoomStatusModel roomStatusModel, RoomStatusModel item) {
	    if (roomStatusModel.getCustomTypeId() == null && roomStatusModel.getCheckinType() == null) {
	        return true;
        }

        if (roomStatusModel.getCustomTypeId() != null) {
	        if (!roomStatusModel.getCustomTypeId().equals(item.getCustomTypeId())) {
	            return false;
            }
        }

        if (roomStatusModel.getCheckinType() != null) {
            if (!roomStatusModel.getCheckinType().equals(item.getCheckinType())) {
                return false;
            }
        }

        return true;
    }

	/**
	 *
	 * 功能：房态筛选，条件或选，条件之间是或者的关系，符合条件的都返回<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private boolean isSelectStatus(RoomStatusModel roomStatusModel, RoomStatusModel item) {
	    if (roomStatusModel.getIsSelEmptyClean() == null && roomStatusModel.getIsSelEmptyDirty() == null
                && roomStatusModel.getIsSelLiveClean() == null && roomStatusModel.getIsSelLiveDirty() == null
                && roomStatusModel.getIsSelRecord() == null && roomStatusModel.getIsSelDueOut() == null
				&& roomStatusModel.getIsSelDebt() == null) {
	        return true;
        }
/*
        if (roomStatusModel.getIsEmpty() != null
                && roomStatusModel.getIsEmpty().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsLive().equals(StatusEnum.FALSE.getValue())
					&& item.getIsDirty().equals(StatusEnum.FALSE.getValue())) {
                return true;
            }
        }*/

        if (roomStatusModel.getIsSelEmptyClean() != null
                && roomStatusModel.getIsSelEmptyClean().equals(StatusEnum.TRUE.getValue())) {
	        if (item.getIsLive().equals(StatusEnum.FALSE.getValue())
					&& item.getIsDirty().equals(StatusEnum.FALSE.getValue())
					&& item.getIsRecord().equals(StatusEnum.FALSE.getValue())) {
	            return true;
            }
        }

        if (roomStatusModel.getIsSelEmptyDirty() != null
                && roomStatusModel.getIsSelEmptyDirty().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsLive().equals(StatusEnum.FALSE.getValue())
					&& item.getIsDirty().equals(StatusEnum.TRUE.getValue())
					&& item.getIsRecord().equals(StatusEnum.FALSE.getValue())) {
                return true;
            }
        }

        if (roomStatusModel.getIsSelLiveClean() != null
                && roomStatusModel.getIsSelLiveClean().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsLive().equals(StatusEnum.TRUE.getValue()) && item.getIsDirty().equals(StatusEnum.FALSE.getValue())) {
                return true;
            }
        }

        if (roomStatusModel.getIsSelLiveDirty() != null
                && roomStatusModel.getIsSelLiveDirty().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsLive().equals(StatusEnum.TRUE.getValue()) && item.getIsDirty().equals(StatusEnum.TRUE.getValue())) {
                return true;
            }
        }

        if (roomStatusModel.getIsSelRecord() != null
                && roomStatusModel.getIsSelRecord().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsRecord().equals(StatusEnum.TRUE.getValue())) {
                return true;
            }
        }

        if (roomStatusModel.getIsSelDueOut() != null
                && roomStatusModel.getIsSelDueOut().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsDueOut().equals(StatusEnum.TRUE.getValue())) {
                return true;
            }
        }

        if (roomStatusModel.getIsSelDebt() != null
                && roomStatusModel.getIsSelDebt().equals(StatusEnum.TRUE.getValue())) {
            if (item.getIsDebt().equals(StatusEnum.TRUE.getValue())) {
                return true;
            }
        }

        return false;
    }

	/**
	 *
	 * 功能：是否可预订房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	private Boolean isReserveRoom(ReserveModel reserveModel, int dayIndex){
		Boolean result = false;

		if (reserveModel.getInTime() == null || reserveModel.getOutTime() == null) {
			return false;
		}

		int beginDay = DateUtil.getDay(reserveModel.getInTime());
		int endDay = DateUtil.getDay(reserveModel.getOutTime());
		int today = DateUtil.getDay();

		if (beginDay <= today + dayIndex && endDay > today + dayIndex) {
			return true;
		}

		return result;
	}

	@Override
	public void getTest() {
		return;
	}
	
	/**
	 * 
	 * 功能: 获得房间状态<br/>
	 * 
	 * 重写：jiangp <br/>
	 * 
	 * @Date ：2017年12月13日 下午6:15:31<br/>
	 * 
	 * @param roomStatusModel
	 * @return <br/>
	 * @see com.yuju.roomStatus.service.IRoomStatusService#getRoomStatus(com.yuju.roomStatus.model.RoomStatusModel)
	 */
	@Override
	public List<RoomStatusModel> getRoomStatus(RoomStatusModel roomStatusModel) {
		// TODO Auto-generated method stub
		return roomStatusDao.getRoomStatus(roomStatusModel);
	}

	/**
	 *
	 * 功能：获得当前房态<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 下午6:14:38 <br/>
	 */
	@Override
	public List<RoomStatusModel> getRoomStatusCurrent(RoomStatusModel roomStatusModel) {
        if (roomStatusModel.getIsEmpty() != null && roomStatusModel.getIsEmpty().equals(StatusEnum.TRUE.getValue())) {
            roomStatusModel.setIsLive(StatusEnum.FALSE.getValue());
        }

		List<RoomStatusModel> list =  roomStatusDao.getRoomStatus(roomStatusModel);

		//当前房态中预离房态也是在住状态
        List<RoomStatusModel> result = new ArrayList<>();
		for (RoomStatusModel item:list ) {
			if (item.getIsDueOut() == StatusEnum.TRUE.getValue()) {
				item.setIsLive(StatusEnum.TRUE.getValue());
			}

			//房态筛选
            if (isStatus(roomStatusModel,item) && isSelectStatus(roomStatusModel, item)) {
                result.add(item);
            }
		}
		return result;
	}

	
	/**
	 * 
	 * 功能: 获取酒店、房型或者指定房间的远期房态
	 * 1.初始化结果数据
	 * 2.查询预订、订单、维修等业务表修正远期房态结果
	 * 
	 * <br/>
	 * 
	 * 重写：jiangp <br/>
	 * 
	 * @Date ：2017年12月6日 下午7:32:30<br/>
	 * 
	 * @param hotelId
	 * @param roomTypeId
	 * @param roomId
	 * @param days
	 * @return <br/>
	 * @see com.yuju.roomStatus.service.IRoomStatusService#getRoomStatusForward(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<Map<String,Object>> getRoomStatusForward(String hotelId, String roomTypeId, 
			String roomId, Integer days){
		//默认远期房态为7天
		if (days == null) {
			days = 7;
		}
		
		//是否单间房的远期房态
		Boolean isSingle = false;
		if (roomId != null) {
			isSingle = true;
		}
		
		//房间信息
		RoomModel roomModel = new RoomModel();
		roomModel.setHotelId(hotelId);
		roomModel.setRoomTypeId(roomTypeId);
		roomModel.setId(roomId);
		roomModel.setIsLock(StatusEnum.FALSE.getValue());
		List<RoomModel> roomList = roomService.selectRoomByRoomType(roomModel);
		
		/*初始化结果数据*/
		Integer roomCount = roomList.size();
		Integer dataStartIndex = 2;//房态数据开始行
		Integer dataStartCol = 2;//房态数据开始列
		if(isSingle) {
			dataStartIndex = 1;
		}
		Integer rowCount = roomCount + dataStartIndex;
		
		List<Map<String, Object>> result = new ArrayList<>(rowCount);

		String curRoomTypeId = null;
		//初始化远期房态数据
		for (int i = 0; i < rowCount; i++) {
			Map<String, Object> rowData = new HashMap<>();
			if (i < dataStartIndex) {
				if (i == 0) {
					rowData.put("roomTypeName", "房型");
					rowData.put("roomNo", "房号");
					Date today = new Date();
					SimpleDateFormat daySDF = new SimpleDateFormat("MM-dd");
					for (int j = 0; j < days; j++) {
						String colName = "day" + String.valueOf(j);
						String dayName;
						Date curDay =  DateUtil.getInterval(today, j);
		                if (j == 0) {
		                	dayName = daySDF.format(curDay) + "(今天)";
		                } else {
		                	dayName = daySDF.format(curDay) + "(" + getWeekFormat(curDay) + ")";
		                }
		                rowData.put(colName, dayName);
					}
				}
				//初始化统计数据
				else {
					rowData.put("roomTypeName", "可售房总数");
					rowData.put("roomNo", "");
					for (int j = 0; j < days; j++) {
						String colName = "day" + String.valueOf(j);
						rowData.put(colName, 0);
					}
				}
			}
			else {
				for (int j = 0; j < days; j++) {
					String colName = "day" + String.valueOf(j);
					rowData.put(colName, ForwardRoomStatusEnum.KONG.getValue());
				}

				int roomDataIndex = i - dataStartIndex;
				//房型
				if (curRoomTypeId == null) {
					curRoomTypeId = roomList.get(roomDataIndex).getRoomTypeId();
					rowData.put("roomTypeName", roomList.get(roomDataIndex).getRoomTypeName());
				}

				if (curRoomTypeId !=null && !curRoomTypeId.equals(roomList.get(roomDataIndex).getRoomTypeId())) {
					rowData.put("roomTypeName", roomList.get(roomDataIndex).getRoomTypeName());
					curRoomTypeId = roomList.get(roomDataIndex).getRoomTypeId();
				}

				//房号
				rowData.put("roomNo", roomList.get(roomDataIndex).getRoomCode());
			}

            result.add(rowData);
		}

		//更新远期房态相关数据
		RoomStatusModel roomStatusModel = new RoomStatusModel();
		roomStatusModel.setHotelId(hotelId);
		roomStatusModel.setRoomTypeId(roomTypeId);
		roomStatusModel.setRoomId(roomId);
		Date endDay = DateUtil.addDays(new Date(), days);
		roomStatusModel.setEndDay(endDay);
		List<RoomStatusModel> forwardRoomStatusList = roomStatusDao.getForwardRoomStatus(roomStatusModel);
		for (RoomStatusModel item:forwardRoomStatusList ) {
			int beginDayIndex = getDayIndex(item.getBeginDay(), days);
			int endDayIndex = getDayIndex(item.getEndDay(), days);
			if (beginDayIndex == INVALID_DAY_INDEX || endDayIndex == INVALID_DAY_INDEX) {
				continue;
			}

			for (int i = dataStartIndex; i < rowCount; i++) {
				Map<String, Object> rowData = result.get(i);
				int roomDataIndex = i - dataStartIndex;
				String rowRoomId = roomList.get(roomDataIndex).getId();
				if (rowRoomId.equals(item.getRoomId())) {
					Byte forwardRoomStatus = item.getForwardRoomStatus();

					//判断是否预订预离房，开始日期的房态为在住房态。
					if (forwardRoomStatus.equals(ForwardRoomStatusEnum.YUDING_KONG.getValue())) {
						String preName = "day" + String.valueOf(beginDayIndex);
						Byte preForwardRoomStatus = Byte.valueOf(rowData.get(preName).toString());
						if (preForwardRoomStatus.equals(ForwardRoomStatusEnum.ZAIZHU.getValue())) {
							forwardRoomStatus = ForwardRoomStatusEnum.YUDING_YULI.getValue();
						}
					}

					for (int j = beginDayIndex; j < endDayIndex; j++) {
						String colName = "day" + String.valueOf(j);

						//如果当前是在住，则在住优先@tobetter
						Byte curForwardStatus = Byte.valueOf(rowData.get(colName).toString());
						if (curForwardStatus != null && curForwardStatus.equals(ForwardRoomStatusEnum.ZAIZHU.getValue())) {
							continue;
						}

						/*
						//加入最后一天为在住，则设置为空房
						if (j == endDayIndex && forwardRoomStatus != null
								&& forwardRoomStatus.equals(ForwardRoomStatusEnum.ZAIZHU.getValue())){
							forwardRoomStatus = ForwardRoomStatusEnum.KONG.getValue();
						}*/
						rowData.put(colName, forwardRoomStatus);
					}

				}
			}
		}

		ReserveModel reserveModel = new ReserveModel();
		reserveModel.setHotelId(hotelId);
		reserveModel.setReserveStatus(ReserveStatusEnum.YDZ.getValue());
		//reserveModel.setInTime(new Date());
		List<ReserveModel> reserveModelList = reserverService.list(reserveModel);

		//更新可售房总数
		if (!isSingle) {
			Map<String,Object> kssRow = result.get(dataStartIndex-1);
			for (int i = 0; i<days; i++) {
				String colName = "day" + String.valueOf(i);
				Integer sum = 0;
				for (int j = dataStartIndex; j < result.size(); j++) {
					Map<String,Object> item = result.get(j);
					if (item.get(colName) == null) {
						continue;
					}

					Byte forwardStatus = Byte.valueOf(item.get(colName).toString());
					if (!(forwardStatus.equals(ForwardRoomStatusEnum.ZAIZHU.getValue())
							|| forwardStatus.equals(ForwardRoomStatusEnum.WEIXIU.getValue()))) {
						sum++;
					}
				}

				for (ReserveModel item:reserveModelList ) {
					if (isReserveRoom(item, i)) {
						sum = sum - item.getRoomCount();
					}
				}

				kssRow.put(colName, sum);

			}
		}


		
		return result;
	}

	/**
	 *
	 * 功能：获取可预订房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月11日 下午7:31:02 <br/>
	 */
	@Override
	public List<RoomStatusModel> getReserveEnableRoom(RoomStatusModel roomStatusModel){
		List<RoomStatusModel> list = getRoomStatus(roomStatusModel);
		List<RoomStatusModel> result = new ArrayList<RoomStatusModel>();
		for (RoomStatusModel item: list) {
			ReserveRoomStatusEnum status = getReserveRoomStatus(item);
			if (!status.equals(ReserveRoomStatusEnum.NO_RESERVE)) {
				item.setReserveRoomStatus(status.getValue());
				result.add(item);
			}
		}

		return result;
	}

	/**
	 *
	 * 功能：获取可入住房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月11日 下午7:31:02 <br/>
	 */
	@Override
	public  List<CheckinRoomModel> getCheckinEnableRoom(CheckinRoomModel checkinRoomModel){
		List<CheckinRoomModel> result = new ArrayList<CheckinRoomModel>();

		//获得房型价格
		RoomTypeStatusModel roomTypeStatusModel = new RoomTypeStatusModel();
		roomTypeStatusModel.setHotelId(checkinRoomModel.getHotelId());
		roomTypeStatusModel.setRoomTypeId(checkinRoomModel.getRoomTypeId());
		roomTypeStatusModel.setCheckinType(checkinRoomModel.getCheckinType());
		roomTypeStatusModel.setCheckinStandardId(checkinRoomModel.getCheckinStandardId());
		roomTypeStatusModel.setPriceSchemeId(checkinRoomModel.getPriceSchemeId());
		roomTypeStatusModel.setCheckinTime(checkinRoomModel.getCheckinTime());
		roomTypeStatusModel.setCheckoutTime(checkinRoomModel.getCheckoutTime());
		List<RoomTypeStatusModel> priceList = roomTypeStatusService.getRoomTypePrice(roomTypeStatusModel);

		//筛选房间
		RoomStatusModel roomStatusModel = new RoomStatusModel();
		roomStatusModel.setHotelId(checkinRoomModel.getHotelId());
		roomStatusModel.setBuildingId(checkinRoomModel.getBuildingId());
		roomStatusModel.setFloorId(checkinRoomModel.getFloorId());
		roomStatusModel.setRoomTypeId(checkinRoomModel.getRoomTypeId());
		roomStatusModel.setRoomId(checkinRoomModel.getRoomId());
		roomStatusModel.setBeginDay(checkinRoomModel.getCheckinTime());
		roomStatusModel.setEndDay(checkinRoomModel.getCheckoutTime());
		List<RoomStatusModel> list = getRoomStatus(roomStatusModel);

        for (RoomStatusModel item: list) {
            ReserveRoomStatusEnum status = getReserveRoomStatus(item);
            if (!status.equals(ReserveRoomStatusEnum.NO_RESERVE)) {
            	CheckinRoomModel roomModel = new CheckinRoomModel();
                item.setReserveRoomStatus(status.getValue());

                roomModel.setHotelId(item.getHotelId());
                roomModel.setBuildingId(item.getBuildingId());
                roomModel.setBuildingName(item.getBuildingName());
                roomModel.setFloorId(item.getFloorId());
                roomModel.setFloorName(item.getFloorName());
                roomModel.setRoomTypeId(item.getRoomTypeId());
                roomModel.setRoomTypeName(item.getRoomTypeName());
                roomModel.setRoomId(item.getRoomId());
                roomModel.setRoomCode(item.getRoomCode());
                roomModel.setReserveRoomStatus(item.getReserveRoomStatus());

                String roomTypeId = item.getRoomTypeId();
				for (RoomTypeStatusModel roomTypePrice: priceList) {
					if (roomTypeId != null && roomTypeId.equals(roomTypePrice.getRoomTypeId())) {
						roomModel.setRoomPrice(roomTypePrice.getRoomPrice());
						roomModel.setDayPrice(roomTypePrice.getDayPrice());
						roomModel.setMonthPrice(roomTypePrice.getMonthPrice());
						roomModel.setLowestMonthPrice(roomTypePrice.getLowestMonthPrice());
						roomModel.setTotalPrice(roomTypePrice.getTotalPrice());
						roomModel.setBreakCount(roomTypePrice.getBreakCount());
					}
				}

				result.add(roomModel);
            }
        }

        return result;
	}

	/**
	 *
	 * 功能：验证当前房间是否可入住。<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月11日 下午7:31:02 <br/>
	 */
	@Override
	public List<RoomStatusModel> verifyCheckinRoom(List<RoomStatusModel> roomStatusModelList, Date beginDay, Date endDay){
		List<RoomStatusModel> result = roomStatusModelList;
		RoomStatusModel roomStatusModel = new RoomStatusModel();
		List<String> roomIdList = new ArrayList<>();

		if (roomStatusModelList != null && roomStatusModelList.size() == 0) {
			return result;
		}

		for (RoomStatusModel item:roomStatusModelList ) {
			//必须传入roomId
			if (item.getRoomId() == null) {
				throw new BusinessException(RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKIN_ROOM_VERIFY_FAILURE.getCode(),
						RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKIN_ROOM_VERIFY_FAILURE.getDesc());
			}

			roomIdList.add(item.getRoomId());
		}
		roomStatusModel.setRoomIdList(roomIdList);
		roomStatusModel.setBeginDay(beginDay);
		roomStatusModel.setBeginDay(endDay);

		List<RoomStatusModel> roomStatusList = this.getRoomStatus(roomStatusModel);
		for (RoomStatusModel item:roomStatusList ) {
			String roomId = item.getRoomId();
			if (roomId == null) {
				continue;
			}

			for (RoomStatusModel verifyRoomStatus:result ) {
				if (roomId.equals(verifyRoomStatus.getRoomId())) {
					verifyRoomStatus.setIsCheckinEnable(isCheckinEnableRoom(item, verifyRoomStatus.getReserveId()));
					break;
				}
			}
		}

		return result;
	}

	private RoomStatusPrintEnum getRoomStatusPrintEnum(RoomStatusModel roomStatusModel){
		if (roomStatusModel == null) {
			return RoomStatusPrintEnum.EMPTY_CLEAN;
		}

		if (roomStatusModel.getIsRecord().equals(StatusEnum.TRUE.getValue())) {
			return RoomStatusPrintEnum.RECORD;
		}else if (roomStatusModel.getIsLive().equals(StatusEnum.FALSE.getValue())
				&& roomStatusModel.getIsDirty().equals(StatusEnum.FALSE.getValue())) {
			return RoomStatusPrintEnum.EMPTY_CLEAN;
		}
		else if (roomStatusModel.getIsLive().equals(StatusEnum.FALSE.getValue())
				&& roomStatusModel.getIsDirty().equals(StatusEnum.TRUE.getValue())) {
			return RoomStatusPrintEnum.EMPTY_DIRTY;
		}
		else if (roomStatusModel.getIsLive().equals(StatusEnum.TRUE.getValue())
				&& roomStatusModel.getIsDirty().equals(StatusEnum.FALSE.getValue())) {
			return RoomStatusPrintEnum.LIVE_CLEAN;
		}
		else if (roomStatusModel.getIsLive().equals(StatusEnum.TRUE.getValue())
				&& roomStatusModel.getIsDirty().equals(StatusEnum.TRUE.getValue())) {
			return RoomStatusPrintEnum.LIVE_DIRTY;
		}

		return RoomStatusPrintEnum.EMPTY_CLEAN;
	}

	/**
	 *
	 * 功能：获取房态打印信息<br/>
	 *
	 * @author jiangp
	 * @Date 2018年2月5日 下午7:31:02 <br/>
	 */
	private static String PREFIX_ROOM_DUE_OUT = "★";
	@Override
	public List<RoomStatusPrintModel> getRoomStatusPrint(RoomStatusModel roomStatusModel) {
		List<RoomStatusModel> queryList = this.getRoomStatusCurrent(roomStatusModel);

		List<RoomStatusPrintModel> result = new ArrayList<RoomStatusPrintModel>();

		if (queryList.size() == 0) {
			return result;
		}

		String curBuildingId = "";
		Integer totalSum = queryList.size();
		for (RoomStatusModel item:queryList ) {
			String buildingId = item.getBuildingId();
			RoomStatusPrintEnum printStatus = getRoomStatusPrintEnum(item);

			if (buildingId != null && !buildingId.equals(curBuildingId)) {
				curBuildingId = buildingId;
				RoomStatusPrintModel newPrintModel = new RoomStatusPrintModel();
				newPrintModel.setRoomStatus(printStatus.getDesc());
				newPrintModel.setPrintEnum(printStatus);
				newPrintModel.setBuildingName(item.getBuildingName());
				if (item.getIsDueOut().equals(StatusEnum.TRUE.getValue())) {
					newPrintModel.setRoomCodes("【" + PREFIX_ROOM_DUE_OUT + item.getRoomCode() + "】");
				}
				else {
					newPrintModel.setRoomCodes("【" + item.getRoomCode() + "】");
				}
				newPrintModel.setRoomCount(1);
				result.add(newPrintModel);
			}
			else {
				//先搜索当前房态记录中是否有

				Boolean isNew = true;
				for (int i = 0; i < result.size(); i++) {
					if (printStatus.getCode() == result.get(i).getPrintEnum().getCode()) {
						Integer roomCount = result.get(i).getRoomCount();

						String roomCodes = "";
						if (item.getIsDueOut().equals(StatusEnum.TRUE.getValue())) {
							roomCodes = result.get(i).getRoomCodes() + " " + "【" + PREFIX_ROOM_DUE_OUT + item.getRoomCode() + "】";
						}
						else {
							roomCodes = result.get(i).getRoomCodes() + " " + "【" + item.getRoomCode() + "】";
						}

						roomCount++;
						result.get(i).setRoomCodes(roomCodes);
						result.get(i).setRoomCount(roomCount);

						isNew = false;
						break;
					}

				}

				if (isNew) {
					RoomStatusPrintModel newPrintModel = new RoomStatusPrintModel();
					newPrintModel.setRoomStatus(printStatus.getDesc());
					newPrintModel.setPrintEnum(printStatus);
					newPrintModel.setBuildingName(item.getBuildingName());
					if (item.getIsDueOut().equals(StatusEnum.TRUE.getValue())) {
						newPrintModel.setRoomCodes("【" + PREFIX_ROOM_DUE_OUT + item.getRoomCode() + "】");
					}
					else {
						newPrintModel.setRoomCodes("【" + item.getRoomCode() + "】");
					}
					newPrintModel.setRoomCount(1);
					result.add(newPrintModel);
				}
			}

		}
		return result;
	}

	@Override
	public byte[] expRoomStatusPrint(RoomStatusModel roomStatusModel)throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		List<RoomStatusPrintModel> list = this.getRoomStatusPrint(roomStatusModel);
		if(Utils.isNull(list)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		int amount = 0;
		for (RoomStatusPrintModel model : list) {
			amount = amount = model.getRoomCount();
		}
		e.createRow(0);
		e.setCell(0, "房态");
		e.setCell(1, "楼栋");
		e.setCell(2, "楼层");
		e.setCell(3, "房号");
		e.setCell(4, "房数");
		e.setColumnWidth(0, 4000);
		e.setColumnWidth(1, 4000);
		e.setColumnWidth(2, 4000);
		e.setColumnWidth(3, 10000);
		e.setColumnWidth(4, 4000);
		
		for (int i = 0; i < list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getRoomStatus());
			e.setCell(1, list.get(i).getBuildingName());
			e.setCell(2, list.get(i).getFloorName());
			e.setCell(3, list.get(i).getRoomCodes());
			e.setCell(4, list.get(i).getRoomCount());
		}
		e.createRow(list.size()+1);
		e.setCell(0, "合计");
		e.setCell(4, amount);
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}

	/**
	 *
	 * 功能：获取房间管理信息<br/>
	 *
	 * @author jiangp
	 * @Date 2018年2月5日 下午7:31:02 <br/>
	 */
	@Override
	public List<RoomStatusModel> getRoomStatusManage(RoomStatusModel roomStatusModel) {
		List<RoomStatusModel> result = this.getRoomStatusCurrent(roomStatusModel);

		for (RoomStatusModel item:result ) {
			RoomStatusPrintEnum printStatus = getRoomStatusPrintEnum(item);
			item.setPrintEnum(printStatus);
			item.setRoomStatus(printStatus.getDesc());
		}

		return result;
	}


}
